import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.*;
//Current Status - The manufacturer method is working. 
//TO DO - Make the other methods return an arraylist for the drop down menu to keep updating.
//TO DO - Make the vendor to be working to.
// TO DO - Insertion,deletion, Update - Add a text area at LAyout.SOUTH

public class ShoppingCart extends JFrame 
{
    private JScrollPane pane = null;

    private JList jlist = null;
    ArrayList<String> values = new ArrayList<String>();// The selected values.
    ArrayList<String> cart = new ArrayList<String>();
    private String selected = "";


    JFrame frame = new JFrame();

    private JComboBox c = new JComboBox();

    private JButton b = new JButton("Add item");


    private int count = 0, p = 0;;
    String manuf ="" ;
    String model = "", year= "", description = "",litres, inches= "", engine= "";

    ArrayList<String> temp;
    //ArrayList<String> temp2;
    private Connection connection;
    private ConnectDb connectDb;


    public ShoppingCart(Connection connection, ConnectDb connectDb) 
    {
        this.connection = connection;
        this.connectDb = connectDb;
     
        shop();
    }

    public void shop()
    {
        c.removeAllItems();
        count = 0;
        final Query q = new Query(connection, connectDb);
        final JPanel displayPanel = new JPanel() ; // the upper panel.
        final JPanel optionPanel = new JPanel() ; // the 2nd panel which displays the content.
        temp  = q.queryManufac();
        int x = temp.size();
        for(int i = 0;i<x;i++)
            c.addItem(temp.get(i));

        ActionListener[] a = b.getActionListeners();
        
        for(int i = 0; i < a.length; i++)
            b.removeActionListener(a[i]);
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) 
            {                       
                selected = (String) c.getSelectedItem();
                System.out.println(selected);
                if(selected == null)
                {
                    selected = "";
                }
                if(count == 0)
                    {
                    System.out.println("modelc"+ count);
                        temp = q.queryModel(selected.trim());
                    }
                else if(count == 1)
                    temp = q.queryYear(selected.trim());
                else if(count == 2)
                {
                    temp = q.queryLitres(selected.trim());
                    printList(temp);
                }
                else if(count == 3)
                {
                    System.out.println("selected:" + selected);
                    temp = q.queryEngineType(selected.trim());
                    printList(temp);
                }
                else if(count == 4)
                    temp = q.queryCubicInches(selected.trim());
                else if(count == 5)
                    temp = q.queryDesc(selected.trim());
                else if(count == 6)
                    temp = q.queryRlink(selected.trim());
                else if(count == 7)
                {
                    count = 0;
                    System.out.println(count);
                    displayPanel.setVisible(false);
                    optionPanel.setVisible(false);
                    final JPanel panel = new JPanel();

                    ArrayList<String> parts = q.queryPNum(temp.get(0));
                    printList(parts);

                    jlist = new JList(parts.toArray());
                    pane = new JScrollPane(jlist);
                    JButton button = new JButton("add to cart");
                    JButton button2 = new JButton("display cart");
                    final JPanel pan = new JPanel();
                    pan.add(button);
                    pan.add(button2);
                    jlist.addListSelectionListener(new ListSelectionListener() {
                        public void valueChanged(ListSelectionEvent e) {
                            if (!e.getValueIsAdjusting()) {
                                selected = (String) jlist.getSelectedValue();

                            }}});

                    button.addActionListener(new ActionListener()
                    {

                        @Override
                        public void actionPerformed(ActionEvent arg0)
                        {
                            
                            if(selected.length() > 8)
                                cart.add(selected);
                            count = 0;
                            //frame.removeAll();
                            frame.setLayout(new BorderLayout());
                            frame.remove(panel);
                            frame.remove(pan);
                            frame.repaint();
                            frame.setVisible(false);
                            shop();
                           

                        }
                    });
                    button2.addActionListener(new ActionListener()
                    {
                        public void actionPerformed(ActionEvent arg0)
                        {
                            System.out.println("display cart");
                            JPanel newPanel = new JPanel();
                            jlist.removeAll();
                            jlist = new JList(cart.toArray());
                            JScrollPane pane = new JScrollPane(jlist);
                            newPanel.add(pane);
                            frame.setVisible(false);
                            JFrame newJFrame = new JFrame();
                            newJFrame.add(newPanel);
                            newJFrame.setVisible(true);
                            newJFrame.pack();
                            frame.repaint();
                            newJFrame.repaint();
                            
                        }
                    });

                    panel.add(pane);
                    panel.setVisible(true);
                    frame.add(panel, BorderLayout.EAST);
                    //frame.add(button, BorderLayout.SOUTH);
                    frame.add(pan,BorderLayout.SOUTH);
                    frame.repaint();
                 
                }

                c.removeAllItems();

                for(int i = 0; i < temp.size(); i++)
                {
                    c.addItem(temp.get(i));
                }

                count++;
            }
        });

        optionPanel.setLayout(new FlowLayout());
        optionPanel.add(c);
        optionPanel.add(b);
        //connectDb.disconnectFromDB();

        frame.setLayout(new BorderLayout());
        frame.add(optionPanel,BorderLayout.NORTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setVisible(true);

    }

    public void printList(ArrayList<String> a)
    {
        for(int i = 0; i < a.size(); i++)
            System.out.println("LIST:" + a.get(i));
    }



}

